<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Material_Model extends CI_Model{
    function __construct()
    {
        parent::__construct();
    }
    function getAllData(){
		$this->db->order_by('nb_order','ASC');
        $query = $this->db->get('material');
        return $query->result();  
    }
    function getNumData(){
        $query = $this->db->get('material');
        return $query->num_rows();
    }
     function get_object($id)
    {
        $query = $this->db->get_where('material', array('material_id' =>$id));
        return $query->row();
    }
	 function get_object_from_product($product_id)
    {
         $this->db->select("material_id");
		$query = $this->db->get_where('material_product', array('product_id' =>$product_id));
		$data_result = array();
		foreach($query->result_array() as $query_item){
				$data_result[]= $query_item['material_id'];
		}
		
        return $data_result;
    }
    function delete($id){
          $this->db->where('material_id',$id);
          $query = $this->db->delete('material');
          if($query){
              return true;
          }else{
              return false;
          }
    }
	 function delete_pro($id){
          $this->db->where('product_id',$id);
          $query = $this->db->delete('material_product');
          if($query){
              return true;
          }else{
              return false;
          }
    }
    function get_list_permit(){
        return $this->db->get('permission')->result();
    }

   function saveData($material_id,$data){
             if($material_id!=0){
                    $this->db->where('material_id',$material_id);
                    if($this->db->update('material',$data)){
                         return true;
                    }else{
                         return false;
                    }
             }else{
                    if($this->db->insert('material',$data)){
                        return true;
                    }else{
                        return false;
                    }
            }
    }
	function saveProductData($material_product_id,$data){
             if($material_product_id!=0){
                    $this->db->where('material_product_id',$material_product_id);
                    if($this->db->update('material_product',$data)){
                         return true;
                    }else{
                         return false;
                    }
             }else{
                    if($this->db->insert('material_product',$data)){
                        return true;
                    }else{
                        return false;
                    }
            }
    }
    function get_num_data($arr_search){
            /*Begin search*/
            switch($arr_search['field_search'])
            {
                    case 'name':
                            $this->db->like('name',$arr_search['key_search']);
                            break ;
                    
            }
            /*End search*/
             $query = $this->db->get('material');
             return $query->num_rows();
    }
    function get_all_data($num,$offset,$arr_search)
    {
            /*Begin search*/
            switch($arr_search['field_search'])
            {
                    case 'name':
                            $this->db->like('name',$arr_search['key_search']);
                            break ;
                    
            }
            /*End search*/
             $this->db->order_by('material_id','DESC');
             $query = $this->db->get('material',$num,$offset);
             return $query->result();
    }
  }
?>
